﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace E.Y.E.Jobs
{
    class CommandRedo : ICommand
    {
        private const string MESSAGE_COMMAND_TITLE = "/=======Redo COMMAND=======\\";
        private const string MESSAGE_SUCCESSFUL_REDO = "Redo.";
        private const string ERROR_MESSAGE_FAIL_REDO = "Unable to Redo.";

        String Message;
        bool ErrorFlag;

        public bool Execute(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            ICommand LastAction = actionsHistory.GetRedoCommand();

            bool EmptyHistory = LastAction == null;
            if (EmptyHistory)
            {
                Message = ERROR_MESSAGE_FAIL_REDO;
                ErrorFlag = true;
                return false;
            }

            bool UndoSuccess = LastAction.Execute(ref taskDatabase, ref CommandTypeDatabase, ref actionsHistory);
            if (UndoSuccess)
            {
                Message = LastAction.getMessage();
                ErrorFlag = LastAction.GetErrorState();
                return true;
            }
            else
            {
                Message = ERROR_MESSAGE_FAIL_REDO;
                ErrorFlag = true;
                return false;
            }
        }

        public bool Undo(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            // Not undo functionality for Redo
            return false;
        }

        public string getCommandEventTitle()
        {
            return MESSAGE_COMMAND_TITLE;
        }

        public void setMessage(string desiredMessage)
        {
            Message = desiredMessage;
        }

        public string getMessage()
        {
            return Message;
        }

        public void SetError(bool desiredBool)
        {
            ErrorFlag = desiredBool;
        }

        public bool GetErrorState()
        {
            return ErrorFlag;
        }

        public InformationPackage ConvertToInfoPackage()
        {
            InformationPackage Result = new InformationPackage();

            if (ErrorFlag)
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.OperationError;
            }
            else
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.Undo;
            }
            Result.Message = Message;

            return Result;
        }
    }
}
